﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.ShpMana
{
    public partial class wfRecordCardEdit : msBsc.Cls.BPageHt
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        static string recordImg = "";
        private void GetRecordInfon()
        {
            string djackId = this.GetParamHtValue("djackId");
            using (msBsc.AppLogic.DataAlc.CBizRecordCard recordCard = new AppLogic.DataAlc.CBizRecordCard())
            {
                DataSet ds = recordCard.GetRecordDjackId(djackId);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                string strRet = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 100, "");
                this.ExecJScript("setRecordInfo(" + strRet + ")");
                initArea(ds.Tables[0].Rows[0]["COUNTRY_ID"].ToString(), ds.Tables[0].Rows[0]["CITY_ID"].ToString(), ds.Tables[0].Rows[0]["AREA_ID"].ToString());
                recordImg = ds.Tables[0].Rows[0]["DOCUMENT_IMAGE"].ToString();
            }
        }

        private void updateRecodrd()
        {
            string recordId = this.GetParamHtValue("recordId");
            string recordNumber = this.GetParamHtValue("recordNumber");
            string recordPicture = this.GetParamHtValue("recordPicture");
            string businessName = this.GetParamHtValue("businessName");
            string countryId = this.GetParamHtValue("countryId");
            string countryName = this.GetParamHtValue("countryName");
            string cityId = this.GetParamHtValue("cityId");
            string cityName = this.GetParamHtValue("cityName");
            string destinationId = this.GetParamHtValue("destinationId");
            string destinationName = this.GetParamHtValue("destinationName");
            string vehicleNumber = this.GetParamHtValue("vehicleNumber");
            string vehicleType = this.GetParamHtValue("vehicleType");
            string seats = this.GetParamHtValue("seats");
            string businessScope = this.GetParamHtValue("businessScope");
            string issuingDate = this.GetParamHtValue("issuingDate");
            string publisherId = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(publisherId))
            {
                this.ExecJScript("layer.alert('请登录！', 3);location.reload();");
            }
            if (string.IsNullOrEmpty(recordId) || string.IsNullOrEmpty(recordNumber) || string.IsNullOrEmpty(recordPicture) || string.IsNullOrEmpty(businessName) || string.IsNullOrEmpty(countryId) || string.IsNullOrEmpty(countryName) || string.IsNullOrEmpty(cityId) || string.IsNullOrEmpty(cityName) || string.IsNullOrEmpty(destinationId) || string.IsNullOrEmpty(destinationName) || string.IsNullOrEmpty(vehicleNumber) || string.IsNullOrEmpty(vehicleType) || string.IsNullOrEmpty(seats) || string.IsNullOrEmpty(businessScope) || string.IsNullOrEmpty(issuingDate))
            {
                this.ExecJScript("layer.alert('请完善备案证信息！', 3);");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizRecordCard record = new AppLogic.DataAlc.CBizRecordCard())
            {
                //drivingLicense.AddDrivingLicense();
                if (recordPicture!=recordImg)
                {
                    using (msBsc.AppLogic.DataAlc.CBizSysFile oCBizSysFile = new msBsc.AppLogic.DataAlc.CBizSysFile())
                    {
                        //保存到系统永久目录，之前上传时存到临时目录的，也可另写代码拷贝文件到其他目录
                        recordPicture = oCBizSysFile.SaveImgToServer(""
                            , msBsc.SvRef.UserInfo.Operator
                            , "1"
                            , "VEH"
                            , ""
                            , recordPicture//文件来源虚拟路径
                            , ""//文件来源物理路径，虚拟和物理只有一个就行
                            , true);
                    }
                }

                string strRet = record.UpdateRecord(recordId, recordNumber, recordPicture, businessName, countryId, countryName, cityId, cityName, destinationId, destinationName, vehicleNumber, vehicleType, seats, businessScope, issuingDate);
                if (strRet != "OK")
                {
                    this.ExecJScript("layer.alert('" + strRet + "', 3);");
                    return;
                }
                this.ExecJScript(@"success();");
            }
        }

        private void getArea2()
        {
            string parentsId = this.GetParamHtValue("parentsId");
            string cityId = "";
            string destionId = "";
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                if (string.IsNullOrEmpty(cityId))
                {
                    cityId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                string strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""selCity"",'" + cityId + "');");

                ds = cbcc.SelectCountryByParentId(cityId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                DataRow dr = ds.Tables[0].NewRow();
                dr["COUNTRY_CITY_ID"] = "";
                dr["COUNTRY_CITY_NME"] = "请选择";
                ds.Tables[0].Rows.InsertAt(dr, 0);
                if (string.IsNullOrEmpty(destionId))
                {
                    destionId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""selDestination"",'" + destionId + "');");
            }
        }
        private void getArea3()
        {
            string parentsId = this.GetParamHtValue("parentsId");
            string destionId = "";
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                DataRow dr = ds.Tables[0].NewRow();
                dr["COUNTRY_CITY_ID"] = "";
                dr["COUNTRY_CITY_NME"] = "请选择";
                ds.Tables[0].Rows.InsertAt(dr, 0);
                if (string.IsNullOrEmpty(destionId))
                {
                    destionId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                string strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""selDestination"",'" + destionId + "');");
            }
        }

        private void initArea(string parentsId, string cityId, string destionId)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                if (string.IsNullOrEmpty(cityId))
                {
                    cityId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                string strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""selCity"",'" + cityId + "');");

                ds = cbcc.SelectCountryByParentId(cityId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                DataRow dr = ds.Tables[0].NewRow();
                dr["COUNTRY_CITY_ID"] = "";
                dr["COUNTRY_CITY_NME"] = "请选择";
                ds.Tables[0].Rows.InsertAt(dr, 0);
                if (string.IsNullOrEmpty(destionId))
                {
                    destionId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""selDestination"",'" + destionId + "');");
            }
        }

        private void showVehicleBrand()
        {
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                DataTable dt = vehicleInfo.GetVehicleBrand();
                if (dt == null || dt.Rows.Count < 1)
                {
                    return;
                }
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 100, "");
                this.ExecJScript(@"setSelect('vehicleBrand'," + retJson + ");");
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                GetRecordInfon();
                showVehicleBrand();
            }
            else if (this.strAction == "upCountry")
            {
                getArea2();
            }
            else if (this.strAction == "upCity")
            {
                getArea3();
            }
            else if (this.strAction == "saveRecodrd")
            {
                updateRecodrd();
            }
            else if (this.strAction == "editCertificate")
            {
                string parentsId = this.GetParamHtValue("parentsId");
                string cityId = this.GetParamHtValue("cityId");
                string destionId = this.GetParamHtValue("destionId");
                initArea(parentsId, cityId, destionId);
            }
        }
    }
}